// pages/receiptDetails/receiptDetails.js
const app = getApp()
var common = require('../../utils/common.js');
var that = "";

//时间控件
const date = new Date()
const years = []
const months = []
const days = []

for (let i = 1990; i <= date.getFullYear(); i++) {
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
  months.push(i)
}

for (let i = 1; i <= 31; i++) {
  days.push(i)
}
Page({

  /**
   * 页面的初始数据
   */

  data: {
    chargeId: '',
    feeList: [{}, {}],
    roomDetail: {}, //房屋详细信息
    feeDetail: {}, //收款信息
    rentalDetail: {}, //租户详细信息
    collectionSchedule: { //收款数据
      showCollectionSchedule: true, //是否显示收款明细
    },

    roomsData: { //房间详细
      showRoomsDetails: false,
    },
    tenantsData: { //租户详细
      showTenantsDetails: false,
    },
    uncharges: "",
    //以下为时间选择变量
    years: years,
    year: date.getFullYear(),
    months: months,
    month: date.getMonth() + 1,
    days: days,
    day: date.getDate(),
    value: [9998, 0, 0],
    modalHidden: true, //默认隐藏moal
    addContract: 'no', //是否是合同跳转
    istz:0,//是否是退租
    showM:'',//显示到账提示
  },

  //时间modal改变监听
  bindChange: function(e) {
    const val = e.detail.value
    this.setData({
      year: this.data.years[val[0]],
      month: this.data.months[val[1]],
      day: this.data.days[val[2]]
    })
  },
  //退租
  surrenderOfTenancy: function() {
    that.setData({
      modalHidden: false,
    })
  },
  //modal确认取消
  //确认
  confirm: function() {
    // this.data.feeList[this.data.WEItemIndex] = this.data.WEItem;
    this.setData({
      // feeList: this.data.feeList,
      modalHidden: true,
    })
    wx.navigateTo({
      url: '../surrenderOfTenancy/surrenderOfTenancy?time=' + that.data.year + '-' + that.data.month + '-' + that.data.day + '&id=' + that.data.roomDetail.id,
    })
  },
  //取消
  cancel: function() {
    this.setData({
      modalHidden: true,
    })
  },

  collectionScheduleClick: function() { //点击收款数据
    this.setData({
      collectionSchedule: {
        showCollectionSchedule: !this.data.collectionSchedule.showCollectionSchedule,
      }
    })
  },
  roomsClick: function() { //点击房间详细
    this.setData({
      roomsData: {
        showRoomsDetails: !this.data.roomsData.showRoomsDetails,
      }
    })
  },
  tenantsClick: function() { //点击租户详细
    this.setData({
      tenantsData: {
        showTenantsDetails: !this.data.tenantsData.showTenantsDetails,
      }
    })
  },
  /**
   * 到账
   */
  receiveMoney: function() {
    var that = this;
    if (this.data.feeDetail.ischarged == 0) {
      wx.showModal({
        title: '到账提示',
        content: '确认到账！',
        success: function(res) {
          if (res.confirm) {
            that.submitReceiveMoney();
          } else if (res.cancel) {}
        }
      })
    }

  },
  submitReceiveMoney: function() {
    var that = this;
    //获取数据
    common._get('/wx/landlord/confirmFee', {
        chargeId: this.data.chargeId
      },
      //成功
      function(res) {
        console.log(res.data);
        if (res.data.success) {
          wx.showModal({
            title: '提示',
            content: '成功到账',
          })
          that.getDataHttps();
          that.refresh();
          app.setRefresh();
          that.tzChange();
        }
      });
  },
  //账单删除
  detele: function() {
    var that = this;
    //获取数据
    wx.showModal({
      title: '提示',
      content: '确定要删除：' + this.data.roomDetail.houseName + '-' + this.data.roomDetail.roomcode + '-' + this.data.rentalDetail.username + '-' + this.data.feeDetail.endDate + '的账单吗',
      success: function(res) {
        if (res.confirm) {
          common._get('/wx/charge/delete', {
              chargeId: that.data.chargeId
            },
            //成功
            function(res) {
              console.log(res.data);
              if (res.data.success) {
                wx.showModal({
                  title: '提示',
                  content: '删除成功',
                })
                wx.navigateBack({})
                that.refresh();
                app.setRefresh();
                that.tzChange();
              }
            });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  //账单编辑
  receiptModify: function() {
    wx.navigateTo({
      url: '../receiptModify/receiptModify?chargeId=' + that.data.chargeId,
    })
    this.tzChange();
  },
  //水电录入
  WEMeterReading: function() {
    wx.navigateTo({
      url: '../WEFeeEditS/WEFeeEditS?chargeId=' + that.data.chargeId,
    })
    this.tzChange();
  },
  //拨打电话
  callPhone: function() {
    wx.makePhoneCall({
      phoneNumber: that.data.rentalDetail.mobile,
    });
  },
  //发送催费
  sendMsg: function(e) {
    if (that.data.feeDetail.ischarged == 0) {
      // if(){

      // }
      var sendType = e.currentTarget.dataset.sendtype
      var sendTypeText = sendType == 'sms' ? '短信' : '微信';
      console.log(sendTypeText)
      //获取数据
      wx.showModal({
        title: '提示',
        content: '确定发送' + sendTypeText + '催费账单',
        success: function(res) {
          if (res.confirm) {
            common._get('/wx/charge/sendMsg', {
                chargeId: that.data.chargeId,
                type: sendType
              },
              //成功
              function(res) {
                if (res.data.success) {
                  wx.showModal({
                    title: '提示',
                    content: res.data.msg,
                  })
                  that.getDataHttps();
                  that.refresh();
                  app.setRefresh();
                }
              });
          } else if (res.cancel) {}
        }
      })
    }
  },
  //当退租改变 退租账单也要跟着改变
  tzChange:function(){
    if(this.data.istz==1){
      wx.setStorageSync("tzChange", "tzChange");
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log("账单:" + options.istz)
    this.setData({
      chargeId: options.chargeid,
    })
    if (options.add_contract) {
      this.setData({
        addContract: options.add_contract,

      })
    } 
    if (options.istz) {
      this.setData({
        istz: options.istz,

      })
    } 
    that = this;
    this.setData({
      value: [9999, this.data.month - 1, this.data.day - 1],
    })
    if (options.showM){
      this.setData({
        showM:options.showM
      })
    }
    // this.getDataHttps();


    // wx.showLoading({
    //   title: '加载中',
    // })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getDataHttps();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    if (that.data.addContract == 'addLenant') {
      console.log("关闭页面")
      wx.navigateBack({
        delta: 1
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.getDataHttps();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  /**
   * 网络请求获取数据
   */
  getDataHttps: function() {
    that = this;
    console.log(this.data.chargeId)
    //获取数据
    common._get('/wx/landlord/detail', {
        chargeId: this.data.chargeId
      },
      //成功
      function(res) {
        if (res.data.success) {
          //获取数据成功
          if (res.data.data.roomDetail.furniture) {
            res.data.data.roomDetail.furniture = JSON.parse(res.data.data.roomDetail.furniture);
          }
          that.setData({
            roomDetail: res.data.data.roomDetail, //房屋详细信息
            feeDetail: res.data.data.feeDetail, //收款信息
            rentalDetail: res.data.data.rentalDetail, //租户详细信息
            // uncharges: res.data.data.uncharges,//历史欠费账单
          })
          if (that.data.addContract == 'yes' || that.data.showM =='showM') {
            that.data.addContract = 'addLenant'
            that.data.showM=='';
            that.receiveMoney();
          }
        }
      });
  },
  //通知刷新
  refresh: function() {
    wx.setStorageSync('refresh', 'refresh');
  },



})